class Solution {
public:
    void recursion(vector<vector<int> >& res, vector<int>& num, vector<int>& temp, vector<int>& vis)
    {
        if (temp.size() == num.size())
        {
            res.push_back(temp);
            return;
        }
        for (int i = 0; i < num.size(); i++)
        {
            if (vis[i])
                continue;
            if (i > 0 && num[i - 1] == num[i] && !vis[i - 1])
                continue;
            vis[i] = 1;
            temp.push_back(num[i]);
            recursion(res, num, temp, vis);
            vis[i] = 0;
            temp.pop_back();
        }
    }

    vector<vector<int> > permuteUnique(vector<int>& num)
    {
        sort(num.begin(), num.end());
        vector<int> vis(num.size(), 0);
        vector<vector<int> > res;
        vector<int> temp;
        recursion(res, num, temp, vis);
        return res;
    }
};
